﻿Imports Npgsql

Public Class Animais
    Private comando As New NpgsqlCommand()
    Private bd As New ConexaoBd()
    Private tblAnimal As DataTable
    Private tRow As DataRow
    Private dset As DataSet
    Private dsetClientes As DataSet
    Private dsetRacas As DataSet
    Private limpar As New LimparDados()


    Private Sub Animais_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Combo Cliente
        Dim sqlClientes As String = "SELECT idcliente, nome FROM clientes UNION SELECT NULL, NULL"
        dsetClientes = bd.carregarDados(sqlClientes, "clientes")
        cboCliente.DataSource = dsetClientes.Tables("clientes").DefaultView
        cboCliente.DisplayMember = "nome"
        cboCliente.ValueMember = "idcliente"
        cboCliente.Sorted = False

        'Combo Racas
        Dim sqlRacas As String = "SELECT idraca, nome FROM racas UNION SELECT NULL, NULL"
        dsetRacas = bd.carregarDados(sqlRacas, "racas")
        cboRaca.DataSource = dsetRacas.Tables("racas").DefaultView
        cboRaca.DisplayMember = "nome"
        cboRaca.ValueMember = "idraca"
        cboRaca.Sorted = False

    End Sub


    Private Sub btnSalvar_Click(sender As System.Object, e As System.EventArgs) Handles btnSalvar.Click
        Dim dtaNasc = mtxtDtaNasc.Text.Split("/")

        Try
            If Val(txtCod.Text.ToString()) > 0 Then
                comando.CommandText = "UPDATE animais SET nome = @nome, idcliente = @idCliente, idraca = @idRaca, datanasc = @dtanasc WHERE idAnimal = @idAnimal;"

                comando.Parameters.Add("@nome", txtNome.Text)
                comando.Parameters.Add("@idCliente", cboCliente.SelectedValue)
                comando.Parameters.Add("@idRaca", cboRaca.SelectedValue)
                comando.Parameters.Add("@dtanasc", dtaNasc(2) + "-" + dtaNasc(1) + "-" + dtaNasc(0))
                comando.Parameters.Add("@idAnimal", txtCod.Text)

                bd.atualizarDados(comando)

            Else
                comando.CommandText = "INSERT INTO animais(idanimal, nome, idcliente, idraca, datanasc) VALUES (DEFAULT, @nome, @idcliente, @idraca, @datanasc);"

                comando.Parameters.Add("@nome", txtNome.Text)
                comando.Parameters.Add("@idCliente", cboCliente.SelectedValue)
                comando.Parameters.Add("@idRaca", cboRaca.SelectedValue)
                comando.Parameters.Add("@datanasc", dtaNasc(2) + "-" + dtaNasc(1) + "-" + dtaNasc(0))


                bd.adicionarDados(comando)


                Dim sql As String = "SELECT MAX(idanimal) id FROM animais"

                'Realizando consulta
                dset = bd.carregarDados(sql, "idAnimal")
                tblAnimal = dset.Tables.Item(0)
                dset.Dispose()
                tRow = tblAnimal.Rows(0)
                Dim id As Integer
                id = CInt(tRow("id").ToString())
                carregarAnimal(id)

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub carregarAnimal(ByVal id As Integer)
        Dim sql As String = "SELECT * FROM animais WHERE idanimal = " + id.ToString()

        'Realizando consulta
        dset = bd.carregarDados(sql, "animal")
        tblAnimal = dset.Tables.Item(0)
        dset.Dispose()

        If tblAnimal.Rows.Count() = 1 Then
            tRow = tblAnimal.Rows(0)

            txtCod.Text = tRow("idAnimal").ToString()
            txtNome.Text = tRow("nome").ToString()
            cboCliente.SelectedValue = tRow("idcliente").ToString()
            cboRaca.SelectedValue = tRow("idraca").ToString()
            mtxtDtaNasc.Text = tRow("datanasc").ToString()

            btnExcluir.Enabled = True
        Else
            MessageBox.Show("Animal Não Encontrado!")
            btnExcluir.Enabled = False
            limpar.Limpar(Me)
        End If


    End Sub

    Private Sub txtCod_Leave(sender As System.Object, e As System.EventArgs) Handles txtCod.Leave
        If txtCod.Text.Length > 0 And Val(txtCod.Text) > 0 Then
            carregarAnimal(Val(txtCod.Text))
        End If
    End Sub

    Private Sub txtCod_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCod.KeyDown
        If e.KeyCode = 13 Then
            txtNome.Focus()
        End If
    End Sub

    Private Sub btnPesquisar_Click(sender As System.Object, e As System.EventArgs) Handles btnPesquisar.Click

        Dim sql As String = "SELECT a.idanimal, a.nome, c.nome cliente, r.nome raca, datanasc FROM animais a INNER JOIN clientes c ON a.idcliente = c.idcliente LEFT JOIN racas r ON a.idraca = r.idraca"
        dset = bd.carregarDados(sql, "animais")

        grdAnimal.DataSource = dset.Tables("animais").DefaultView


        grdAnimal.Columns(0).HeaderText = "Código"
        grdAnimal.Columns(1).HeaderText = "Nome"
        grdAnimal.Columns(2).HeaderText = "Cliente"
        grdAnimal.Columns(3).HeaderText = "Raça"
        grdAnimal.Columns(4).HeaderText = "Data Nasc."
    End Sub

    Private Sub grdAnimal_RowHeaderMouseDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdAnimal.RowHeaderMouseDoubleClick

        carregarAnimal(grdAnimal.CurrentRow.Cells(0).Value)
        TabControl1.SelectTab(0)
        txtNome.Focus()

    End Sub

    Private Sub btnNovo_Click(sender As System.Object, e As System.EventArgs) Handles btnNovo.Click
        limpar.Limpar(Me)
        txtNome.Focus()
    End Sub

    Private Sub btnExcluir_Click(sender As System.Object, e As System.EventArgs) Handles btnExcluir.Click
        comando.CommandText = "DELETE FROM animais WHERE idAnimal = @idAnimal"
        comando.Parameters.Add("@idAnimal", txtCod.Text)
        bd.atualizarDados(comando)
        limpar.Limpar(Me)
    End Sub

End Class